package com.yunchao.feature.business.presentation.screen.info

import androidx.lifecycle.viewModelScope
import com.yunchao.feature.base.domain.result.errorMsg
import com.yunchao.feature.base.presentation.nav.NavManager
import com.yunchao.feature.base.presentation.viewmodel.BaseAction
import com.yunchao.feature.base.presentation.viewmodel.BaseEffect
import com.yunchao.feature.base.presentation.viewmodel.BaseState
import com.yunchao.feature.base.presentation.viewmodel.BaseViewModel
import com.yunchao.feature.business.data.datasource.api.request.BusinessRequest
import com.yunchao.feature.business.domain.enum.BusinessType
import com.yunchao.feature.business.domain.model.BusinessInfoModel
import com.yunchao.feature.business.domain.repository.BusinessRepository
import com.yunchao.feature.business.presentation.screen.info.BusinessInfoViewModel.Action
import com.yunchao.feature.business.presentation.screen.info.BusinessInfoViewModel.UiEffect
import com.yunchao.feature.business.presentation.screen.info.BusinessInfoViewModel.UiState
import kotlinx.coroutines.launch

internal class BusinessInfoViewModel(
    private val businessRepository: BusinessRepository,
    private val navManager: NavManager
) : BaseViewModel<UiState, Action, UiEffect>(UiState.Idle) {

    fun getBusinessInfo() = viewModelScope.launch {
        postSideEffect(UiEffect.ShowLoading)
        businessRepository.getBusinessInfo(
            BusinessRequest(
                BusinessType.BusinessTypeMIMISIF100.type,
                BusinessType.BusinessTypeMIMISIF100.describe
            )
        ).onSuccess {
            sendAction(Action.LoadBusinessSuccess(it)).apply {
                postSideEffect(UiEffect.HideLoading)
            }
        }.onFailure {
            sendAction(Action.BusinessInfoInit).apply {
                postSideEffect(UiEffect.ShowToast(it.errorMsg))
            }.apply {
                postSideEffect(UiEffect.HideLoading)
            }
        }
    }

    fun navigateToDetails(model: BusinessInfoModel) {
        navManager.navigate(
            BusinessInfoFragmentDirections.actionBusinessInfoFragmentToBusinessDetailsFragment(
                tranTypeId = model.tranTypeID,
                tranId = model.tranID
            )
        )
    }

    internal sealed interface Action : BaseAction<UiState> {
        class LoadBusinessSuccess(private val businessModel: BusinessInfoModel) : Action {
            override fun reduce(state: UiState) = UiState.Success(businessModel)
        }

      data  object BusinessInfoInit : Action {
            override fun reduce(state: UiState) = UiState.Idle
        }
    }

    internal sealed interface UiState : BaseState {
        data class Success(val businessModel: BusinessInfoModel) : UiState
        data object Idle : UiState
    }

    internal sealed interface UiEffect : BaseEffect {
        data object ShowLoading : UiEffect
        data object HideLoading : UiEffect
        data class ShowToast(val msg: String) : UiEffect
    }
}